package com.youlu.campus.web.user.controller;

import com.youlu.campus.common.utils.TimeUtils;
import com.youlu.campus.entity.ActivityTaskInfo;
import com.youlu.campus.entity.UserActivityTaskInfo;
import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.VO.req.ActivityTaskCaDomain;
import com.youlu.campus.entity.VO.req.UserActivityTaskInfoQueryVO;
import com.youlu.campus.entity.domain.SubsriptionWechatMsg;
import com.youlu.campus.entity.domain.UserActivityTaskInfoDomin;
import com.youlu.campus.entity.english.vo.request.QueryQaHomeCommand;
import com.youlu.campus.entity.enums.TaskBizType;
import com.youlu.campus.service.activity.ActivityTaskService;
import com.youlu.campus.service.activity.UserActivityTaskInfoService;
import com.youlu.campus.service.sms.UserSubTemplateMsgService;
import com.youlu.campus.service.visitor.VisitorUserService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Objects;

@Slf4j
@RestController
public class UserActivityTaskController {
    @Autowired
    private UserActivityTaskInfoService userActivityTaskInfoService;
    @Autowired
    private VisitorUserService visitorUserService;

    @Autowired
    private UserSubTemplateMsgService userSubTemplateMsgService;
    @Lazy
    @Autowired
    private ActivityTaskService activityTaskService;

    /**
     * 用户参加任务
     *
     * @return
     */
    @GetMapping("/mini-app/user-task/join")
    public QResult joinTask(@RequestParam("activityId") String activityId,
                            @RequestParam("taskBizType") TaskBizType taskBizType) {
        String province = visitorUserService.getUserProvince(activityId, RequestContext.getUserId());
        UserPO userPO = RequestContext.getCurUser();
        userPO.setProvince(province);
        return QResult.success(userActivityTaskInfoService.join(userPO, activityId, taskBizType));
    }

    /**
     * 用户订阅微信模版消息
     *
     * @return
     */
    @PostMapping("/mini-app/user-task/sub-wechat")
    public QResult subWechatNotify(@RequestBody SubsriptionWechatMsg req) {
        String curPlatform = RequestContext.getCurPlatform();
        if (StringUtils.isBlank(req.getAppId())) {
            req.setAppId(RequestContext.getAppId());
            log.warn(":>>> 用户订阅微信模版消息未携带appId,设置默认的appId:{}", RequestContext.getAppId());
        }
        if (StringUtils.isBlank(req.getSubAppId())) {
            req.setSubAppId(RequestContext.getAppId());
            log.warn(":>>> 用户订阅微信模版消息未携带subAppId,设置默认的subAppId:{}", RequestContext.getAppId());
        }
        if (StringUtils.isBlank(req.getSubOpenId())) {
            req.setSubOpenId(RequestContext.getCurUser().getOpenId());
            log.warn(":>>> 用户订阅微信模版消息未携带SubOpenId,设置默认的SubOpenId:{}", RequestContext.getCurUser().getOpenId());
        }
        if (StringUtils.isBlank(req.getOpenId())) {
            req.setOpenId(RequestContext.getCurUser().getOpenId());
            log.warn(":>>> 用户订阅微信模版消息未携带OpenId,设置默认的OpenId:{}", RequestContext.getCurUser().getOpenId());
        }
//        if(!"h5".equals(curPlatform)){
//            req.setAppId(RequestContext.getAppId());
//        }
        req.setPlatform(RequestContext.getCurPlatform());
        String province = visitorUserService.getUserProvince(req.getActivityId(), RequestContext.getUserId());
        UserPO userPO = RequestContext.getCurUser();
        userPO.setProvince(province);
        userPO.setAppId(req.getAppId());
        req.setUserPO(userPO);
        return QResult.success(userActivityTaskInfoService.subsriptionWechatMsg(req));
    }


    /**
     * 用户订阅平台微信模版消息
     *
     * @return
     */
    @PostMapping("/mini-app/user-task/sub-app-wechat")
    public QResult subAppWechatNotify(@RequestBody SubsriptionWechatMsg req) {
        req.setAppId(RequestContext.getAppId());
        req.setPlatform(RequestContext.getCurPlatform());
        String province = visitorUserService.getUserProvince(req.getActivityId(), RequestContext.getUserId());
        UserPO userPO = RequestContext.getCurUser();
        userPO.setProvince(province);
        req.setUserPO(userPO);
        return QResult.success(userActivityTaskInfoService.subsriptionAppWechatMsg(req));
    }

    /**
     * 用户订阅平台微信模版消息
     *
     * @return
     */
    @PostMapping("/mini-app/user-task/sub-app-wechat/new")
    public QResult subAppWechatNotifyNew(@RequestBody SubsriptionWechatMsg req) {
        req.setAppId(RequestContext.getAppId());
        req.setPlatform(RequestContext.getCurPlatform());
        String province = visitorUserService.getUserProvince(req.getActivityId(), RequestContext.getUserId());
        UserPO userPO = RequestContext.getCurUser();
        userPO.setProvince(province);
        req.setUserPO(userPO);
        return QResult.success(userActivityTaskInfoService.subWechatMsg(req));
    }

    /**
     * 获取用户任务列表
     *
     * @return
     */
    @PostMapping("/mini-app/user/task")
    public QResult userTaskInfos(@RequestBody @NotNull UserActivityTaskInfoQueryVO req) {
        req.setPlatform(RequestContext.getCurPlatform());
        req.setAppId(RequestContext.getAppId());
        req.setDayTime(TimeUtils.getDayStartTime(new Date()));
        req.setUserId(RequestContext.getUserId());
        return QResult.success(userActivityTaskInfoService.findBy(req));
    }

    /**
     * 获取用户订阅消息状态
     *
     * @return
     */
    @GetMapping("/mini-app/user/sub-msg/status")
    public QResult getUserSubMsg() {
        return QResult.success(userSubTemplateMsgService.getSumStatus(RequestContext.getCurUser()));
    }

    /**
     * 用户订阅操作
     *
     * @param subMsgType 1、取消订阅
     *                   2、打开订阅
     * @return
     */
    @PutMapping("/mini-app/user/sub-msg/{subMsgType}")
    public QResult getUserSubMsg(@PathVariable Integer subMsgType) {
        userSubTemplateMsgService.subMsg(RequestContext.getCurUser(), subMsgType);
        return QResult.success(null);
    }

    /**
     * 用户订阅微信模版消息
     *
     * @return
     */
    @PostMapping("/mini-app/user-task/sub-wechat-msg")
    public QResult subWechatMsgNotify(@RequestBody SubsriptionWechatMsg req) {
        String curPlatform = RequestContext.getCurPlatform();
        if (StringUtils.isBlank(req.getAppId())) {
            req.setAppId(RequestContext.getAppId());
            log.warn(":>>> 用户订阅微信模版消息未携带appId,设置默认的appId:{}", RequestContext.getAppId());
        }
        if (StringUtils.isBlank(req.getSubAppId())) {
            req.setSubAppId(RequestContext.getAppId());
            log.warn(":>>> 用户订阅微信模版消息未携带subAppId,设置默认的subAppId:{}", RequestContext.getAppId());
        }
        if (StringUtils.isBlank(req.getSubOpenId())) {
            req.setSubOpenId(RequestContext.getCurUser().getOpenId());
            log.warn(":>>> 用户订阅微信模版消息未携带SubOpenId,设置默认的SubOpenId:{}", RequestContext.getCurUser().getOpenId());
        }
        if (StringUtils.isBlank(req.getOpenId())) {
            req.setOpenId(RequestContext.getCurUser().getOpenId());
            log.warn(":>>> 用户订阅微信模版消息未携带OpenId,设置默认的OpenId:{}", RequestContext.getCurUser().getOpenId());
        }
//        if(!"h5".equals(curPlatform)){
//            req.setAppId(RequestContext.getAppId());
//        }
        req.setPlatform(RequestContext.getCurPlatform());
        String province = visitorUserService.getUserProvince(req.getActivityId(), RequestContext.getUserId());
        UserPO userPO = RequestContext.getCurUser();
        userPO.setProvince(province);
        userPO.setAppId(req.getAppId());
        req.setUserPO(userPO);
        return QResult.success(userActivityTaskInfoService.subsriptionWechatMsg(req));
    }

    /**
     * 用户活动任务详情
     *
     * @return
     */
    @PostMapping("/mini-app/user-task/detail")
    public QResult getUserActivityTask(@RequestBody UserActivityTaskInfoQueryVO req) {
        String curPlatform = RequestContext.getCurPlatform();
        if (StringUtils.isBlank(req.getAppId())) {
            req.setAppId(RequestContext.getAppId());
            log.warn(":>>> 用户活动任务未携带appId,设置默认的appId:{}", RequestContext.getAppId());
        }
        ActivityTaskInfo activityTaskInfo = activityTaskService.find(req.getActivityId(), req.getTaskBizType());
        UserActivityTaskInfo userActivityTaskInfo = userActivityTaskInfoService.find(req.getUserId(), req.getActivityId(), req.getTaskBizType());
        UserActivityTaskInfoDomin domin = new UserActivityTaskInfoDomin();
        if (Objects.isNull(userActivityTaskInfo)) {
            domin.setStatus(0);
            domin.setActivityId(req.getActivityId());
        } else {
            domin.setStatus(userActivityTaskInfo.getStatus());
        }
        if (Objects.nonNull(activityTaskInfo)) {
            domin.setDetail(activityTaskInfo.getDetail());
        }
        return QResult.success(domin);
    }

    /**
     * 提交感悟
     *
     * @return
     */
    @PostMapping("/mini-app/user-task/submit-ganwu")
    public QResult submitGanWu(@RequestParam("activityId") String activityId,
                               @RequestParam("taskBizType") TaskBizType taskBizType) {
        String curPlatform = RequestContext.getCurPlatform();
        return QResult.success(userActivityTaskInfoService.submitGanWu(activityId, RequestContext.getCurUser(), taskBizType));
    }

    /**
     * 获取感悟任务详情
     *
     * @return
     */
    @PostMapping("/mini-app/user-task/find-one")
    public QResult findOne(@RequestParam("activityId") String activityId,
                           @RequestParam("taskBizType") TaskBizType taskBizType) {
        String curPlatform = RequestContext.getCurPlatform();
        UserActivityTaskInfo taskInfo = userActivityTaskInfoService.findUserActivityTaskInfo(activityId, RequestContext.getCurUser(), taskBizType);
        return QResult.success(taskInfo);
    }

    /**
     * 获取感悟任务详情
     *
     * @return
     */
    @PostMapping("/mini-app/user-task/ca-info")
    public QResult findOne(@RequestBody QueryQaHomeCommand req) {
        String curPlatform = RequestContext.getCurPlatform();
        req.setCurrentUser(RequestContext.getCurUser());
        ActivityTaskCaDomain res = userActivityTaskInfoService.getActivityTaskCaInfo(req);
        return QResult.success(res);
    }

}
